package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : BankCardActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 17:58
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.BankCardAdapter;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.MemberBank;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BankCardActivity extends BaseActivity {
    @BindView(R.id.rv_bank_card_list)
    RecyclerView rvBankCardList;
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_add_card)
    Button btnAddCard;

    private BankCardAdapter bankCardAdapter;
    private List<MemberBank.bank> datas = new ArrayList<>();


    private int type = 0; // 0,点击跳转解绑 1 点击跳转详情


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        tvTitle.setText("我的银行卡");
        initListener();
        initOneRecycleView();
//        loadData();
        type = getIntent().getIntExtra("type", 0);
    }

    private void initListener() {
        backTv.setOnClickListener(view -> {
            finish();
        });
        btnAddCard.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, PayPasswordCheckActivity.class);
            intent.putExtra("activity_title", "添加银行卡第一步");
            intent.putExtra("message", "请输入支付密码,以验证身份");
            intent.putExtra("title", "添加银行卡");
            intent.putExtra("imp_type", 2);
            startActivity(intent);
        });
    }


    private void loadData() {
//         加载底部数据
        NetworkManager.INSTANCE.post(Apis.member_bank, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                datas =  MemberBank.get_bank.parseFrom(response).getArrayList();
                bankCardAdapter.replaceData(datas);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }


    /**
     * 银行卡列表
     * *
     */
    private void initOneRecycleView() {
        //     设置布局控制器
        //  reverseLayout 是否实现反转
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rvBankCardList.setLayoutManager(layoutManager);
        rvBankCardList.setAdapter(bankCardAdapter = new BankCardAdapter(mContext, datas));
        bankCardAdapter.bindToRecyclerView(rvBankCardList);
        bankCardAdapter.setEmptyView(R.layout.bank_card_empty);
        bankCardAdapter.setOnItemClickListener((adapter, view, position) -> {
            MemberBank.bank item = datas.get(position);
            if (type == 0) {
                Intent intent = new Intent(mContext, UnBindBankActivity.class);
                intent.putExtra("bank_name", item.getBankName());
                intent.putExtra("bank_number", item.getBankCard());
                intent.putExtra("bank_id", item.getCardId() + "");
                startActivity(intent);
            } else if (type == 1)  // 选择银行卡需要
            {
                Intent intent = new Intent();
                intent.putExtra("info", GsonUtils.serializedToJson(item));
                setResult(TiXianActivity.BACKTOWITHDRAW, intent);
                finish();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
